using System.Collections.Generic;
using UnityEngine;

public class GameplayAchievementsLayer : MonoBehaviour
{
	public UILabel totalAchievements;

	public UITable table;

	public UIScrollView view;

	public GameplayAchievementButton buttonPrefab;

	private List<GameplayAchievementButton> buttons = new List<GameplayAchievementButton>();

	private void OnEnable()
	{
		GenerateAchievementTable();
	}

	private void GenerateAchievementTable()
	{
		List<ReferencableObject> achievements = GGStoryAchivementsController.instance.achievements;
		int num = 0;
		int num2 = 0;
		for (int i = 0; i < achievements.Count; i++)
		{
			if (i >= buttons.Count)
			{
				GameplayAchievementButton component = NGUITools.AddChild(table.gameObject, buttonPrefab.gameObject).GetComponent<GameplayAchievementButton>();
				component.gameObject.name = GGFormat.IndexName(i);
				buttons.Add(component);
			}
			buttons[i].gameObject.SetActive(true);
			GameplayAchievement value = null;
			GGStoryAchivementsController.instance.achievementsMap.TryGetValue(achievements[i].id, out value);
			buttons[i].Init(value);
			num += value.achievedStagesCount;
			num2 += value.totalStages;
		}
		for (int j = achievements.Count; j < buttons.Count; j++)
		{
			buttons[j].gameObject.SetActive(false);
		}
		table.Reposition();
		if (achievements.Count > 0)
		{
			UITools.AlignToLeftOnScroll(buttons[0].gameObject.GetComponent<UIWidget>(), view, -10f);
		}
		UITools.ChangeText(totalAchievements, num + "/" + num2);
	}
}
